﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OAuth;
using System.Web.Script.Serialization;
using Darren.Common.Helper;
using OAuth.Models;

public partial class Sina_Default : System.Web.UI.Page
{
    SinaOAuth auth = new SinaOAuth();
    protected void Page_Load(object sender, EventArgs e)
    {
        string oauth = CookieHelper.GetCookieValue("SinaOAuth");
        string token = StringHelper.QueryString(oauth, "Token");
        string token_secret = StringHelper.QueryString(oauth, "TokenSecret");
        string openId = StringHelper.QueryString(oauth, "OpenID");
        Response.Write(string.Format("access_oauth_token={0}<br/>access_oauth_token_secret={1}<br/>OpenID={2}", token, token_secret, openId));

        auth.UserRemoteIP = Request.UserHostAddress;
        auth.Token = new AccessToken(token, token_secret, openId);
    }
    protected void getFriendsIDs_Click(object sender, EventArgs e)
    {
        string uid = txtFindFriendUID.Text;
        FriendTree tree = buildTree(auth,1857336062,2);
               
    }

    private FriendsList getFriendsList(long uid, SinaOAuth auth)
    {
        string info = auth.GetFriends(uid.ToString());
        FriendsList list = new JavaScriptSerializer().Deserialize<FriendsList>(info);
        return list;
    }
    
    private FriendTree buildTree(SinaOAuth auth,long uid,int dept)
    {
        FriendsList list = getFriendsList(uid,auth);

        FriendTree tree = new FriendTree();

        tree.Root = buildNode(uid, null, auth);

        //dept==2
        if (tree.Root.Children != null)
        {
            for (int i = 0; i < tree.Root.Children.Count; i++)
            {
                tree.Root.Children[i] = buildNode(tree.Root.Children[i].ID, tree.Root, auth);
            }
        }    

        ////dept==3
        //if (tree.Root.Children != null)
        //{
        //    for (int i = 0; i < tree.Root.Children.Count; i++)
        //    {
        //        if (tree.Root.Children[i].Children!=null)
        //        {
        //            for (int j = 0; j < tree.Root.Children[i].Children.Count; j++)
        //            {
        //                FriendTreeNode curNode = tree.Root.Children[i].Children[j];
        //                curNode = buildNode(curNode.ID, tree.Root.Children[i], auth);
        //            }
        //        }
        //    }
        //}
        
        return tree;
    }

    private FriendTreeNode buildNode(long uid,FriendTreeNode parent,SinaOAuth auth)
    {
        FriendTreeNode node = new FriendTreeNode();
        node.ID = uid;
        node.Parent = parent;
        
        FriendsList children = getFriendsList(uid, auth);
        if (children.IDs.Count>0)
        {
            node.Children = new List<FriendTreeNode>();
            foreach (long child in children.IDs)
            {
                node.Children.Add(new FriendTreeNode() { ID = child, Parent = node });
            }
        }       
        return node;
    }
}